#pragma once
#include<iostream>
#include<pthread.h>
#include<unistd.h>
#include<queue>
#include<string.h>
#include<stdlib.h>
using namespace std;

class Task
{
private:
    int _sock;
public:
    Task(int sock = -1):_sock(sock)
    {}
    int Run()
    {
        while (true)
        {
            char buffer[1024] = {0};
            memset(buffer, 0, sizeof(buffer));
            // tcp协议是一种流式服务，可以使用文件操作
            ssize_t s = read(_sock, buffer, sizeof(buffer) - 1);
            if (s > 0)
            {
                buffer[s] = '\0';
                cout << "client# " << buffer << endl;

                string echo_string = ">>>server<<< ";
                echo_string += buffer;

                write(_sock, echo_string.c_str(), echo_string.size());
            }
            else if (s == 0) //对应的客户端退出时，read就会读到0
            {
                cout << "client quit....." << endl;
                break;
            }
            else
            {
                cerr << "read error" << endl;
                break;
            }
        }
        close(_sock);
    }

    ~Task()
    {}
};